package com.qdu.mr;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class CountReducer
        // 实现Reducer类
        extends Reducer<Text, LongWritable, Text, LongWritable> {

    // 针对同一分区中的同一个key，reduce方法执行一次
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {

        // 迭代values，对所有的值进行累加
        Long count = 0l;
        for (LongWritable value : values) {
            count += value.get();
        }
        context.write(key, new LongWritable(count));
    }
}
